import 'package:flutter/material.dart';
import 'package:flutter_app/selfPlugin/selfUpAndDown.dart';

import 'component/messageListCom.dart';

class MessagePage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _messageState();
  }

}
// ignore: camel_case_types
class _messageState extends State{
  ScrollController mController;
  bool _hasPage = true;
  List newListData = [
    {"title":"系统信息1","content":"app软件即将更新！","time":"11:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"12:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"13:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"14:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"15:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"16:20"},
    {"title":"系统信息11","content":"app软件即将更新！","time":"17:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"18:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"19:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"20:20"},
    {"title":"系统信息1","content":"app软件即将更新！","time":"21:20"}
  ];
  int num = 1;
  List listData = [
    {"title":"系统信息","content":"app软件即将更新！","time":"11:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"12:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"13:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"14:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"15:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"16:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"17:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"18:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"19:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"20:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"21:20"}
  ];

  @override
  void initState() {
    mController = ScrollController();
    super.initState();
  }
  @override
  void dispose() {
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.white,
        appBar: PreferredSize(child:
        AppBar(
          title: Text('信息'),
          centerTitle: true,
        ), preferredSize: Size.fromHeight(44)),
        body: SelfUpAndDown(
          sController:mController,
          buildItem: _bulidItem,
          listData: listData,
          startDownFresh: _startDownFresh,
          startUpFresh:_startUpFresh,
          hasPage: _hasPage,
        )
    );
  }


  _bulidItem(index){
    //创建列表
    return Container(
      padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
      margin: EdgeInsets.only(top:10),
      color: Color.fromRGBO(245, 245, 245, 1),
      child: MessageListCom(listData[index]['title'],listData[index]['content'],listData[index]['time']),
    );
  }
  //开始下拉
  Future _startDownFresh(){
    return Future.delayed(Duration(seconds: 3),(){
      var list = new List();
      list.addAll(this.newListData);
      setState(() {
        this.listData = list;
        this.num = 1;
        this._hasPage = true;
      });
    });
  }
  //开始上拉
  Future _startUpFresh(){
    return Future.delayed(Duration(seconds: 1),(){
      if(this.num > 2){
        setState(() {
          this._hasPage = false;
        });
      }else{
        setState(() {
          this.listData.add({"title":"测试","content":"app软件即将更新！","time":"20:20"});
          this.num++;
          this._hasPage = true;
        });
      }
    });
  }

}